<template>
  <div>
    <el-tree
      ref="tree"
      :default-expanded-keys="expandedkeys"
      :highlight-current="true"
      lazy
      :load="loadNode"
      node-key="id"
      :props="props"
      @node-click="nodeclick"
    >
      <template #default="{ node, data }">
        <!-- <span v-if="data.gtel">{{ `${data.mc}(${data.gtel})` }}</span> -->
        <slot :data="data" :node="node">{{ node.label }}({{ data.sum }})</slot>
      </template>
      <!-- <slot></slot> -->
    </el-tree>
  </div>
</template>

<script>
  export default {
    name: 'Pcstree',
    props: {
      gtelfalg: {
        type: Boolean,
        default: false,
      },
      pcsid: {
        type: Object,
        default: () => {
          return {}
        },
      },
      expandedkeys: {
        type: Array,
        default: () => {
          return []
        },
      },
      searchdata: {
        type: Array,
        default: () => {
          return []
        },
      },
    },
    emits: ['nodeclick'],
    data() {
      return {
        props: {
          label: 'pcs',
          children: 'pcschildren',
          isLeaf: 'isLeaf',
        },

        treedata: [],
      }
    },

    created() {},
    mounted() {},
    methods: {
      nodeclick(item) {
        this.$emit('nodeclick', item)
      },
      async loadNode(node, resolve) {
        let r = []
        if (node.level == 0) {
          this.treedata = []
          r = await this.$z.request('/CityStation', {
            sid: `id in (${this.$store.state.user.token.pcs})`,
          })
          this.$store.commit('citystation/setCitystation', r)
        } else if (
          this.searchdata.find((e) => {
            return e.pid == node.data.id
          })
        ) {
          r = this.searchdata.filter((e) => {
            return e.pid == node.data.id
          })
        } else {
          if (this.gtelfalg) {
            r = await this.$z.request('/CityStation', { pid: node.data.id })
            this.$store.commit('citystation/setCitystation', r)
          } else {
            if (node.data.type == 4) {
              r = []
            } else {
              r = await this.$z.request('/CityStation', { pid: node.data.id })

              this.$store.commit('citystation/setCitystation', r)
            }
          }
        }
        this.treedata = this.treedata.concat(r)
        // console.log(r, node.data)
        if (node.data.type == 5) {
          let CityCharge = await this.$z.request('/CityCharge', {
            spcs: `pcs in (${node.data.id})`,
          })
          CityCharge.forEach((e) => {
            e.isLeaf = true
          })
          r = r.concat(CityCharge)
        }
        return resolve(r)
      },
      getCurrentNode() {
        return this.$refs.tree.getCurrentNode()
      },
      setCurrentKey() {},
    },
  }
</script>
<style>
  .el-tree-node__content {
    height: 50px;
  }
</style>
